<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <title>跟随手指的box</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            width: 90%;
            height: 60%;
            border-radius: 15px;
            background-color: aquamarine;
            position: fixed;
            left: 5%;
            bottom: -60%;
            transition: all .3s ease;
        }

        .box .touch_start {
            width: 100%;
            height: 50px;
            background-color: transparent;
            border-bottom: 1px dashed black;
        }

        button {
            width: 60px;
            height: 60px;
            font-size: 16px;
        }
    </style>
</head>
<body>
<div class="box">
    <div class="touch_start"></div>
</div>
<button>点击</button>
<script>
    let oBox = document.querySelector('.box');
    let oBtn = document.querySelector('button');
    let touchStart = document.querySelector('.touch_start');
    let state = false;
    oBtn.addEventListener('click', () => {
        state === false ? oBox.style.bottom = '20px' : oBox.style.bottom = '-60%';
        state = !state;
    }, false);
    let lastY = 0;
    let canMove = false;
    let distance = 0;
    touchStart.addEventListener('touchstart', (e) => {
    	e.preventDefault();
        canMove = true;
        oBox.style.transition = 'none';
        console.log(e.target);
        let touch = e.touches[0];
        // console.log(touch.clientY)
        lastY = touch.clientY;
        console.log(lastY);
    }, false);
    touchStart.addEventListener('touchmove', (e) => {
        if (canMove) {
            let touch = e.touches[0];
            distance = touch.clientY - lastY;
            console.log(distance)
            if (distance < 0) return;
            oBox.style.bottom = -distance + 'px';
        }
    }, false);
    console.log('ontouchstart' in document)
    touchStart.addEventListener('touchend', (e) => {
        oBox.style.transition = 'all .3s ease-in-out';
        if (distance > 100) {
            oBox.style.bottom = '-60%';
            state = !state;
        } else {
            oBox.style.bottom = '20px'
        }

    }, false);
</script>
</body>
</html>
